package com.cat.slidingWindow;

import java.util.Arrays;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/friends-of-appropriate-ages/description/
 * @create 2025/9/17 19:38
 * @since JDK17
 */

public class Solution39 {
    public int numFriendRequests(int[] ages) {
        int ans = 0, n = ages.length;
        int[] cnt = new int[121];
        for (int age : ages) {  // 计数
            cnt[age]++;
        }
        int total = 0;
        for (int x = 0, y = 0; x < 121; x++) {
            total += cnt[x];
            if (2 * y <= x + 14) {
                total -= cnt[y++];
            }
            if (total > 1) {
                ans += (total - 1) * cnt[x];
            }
        }
        return ans;
    }
}
